<template>
  <div class="p-d">
    <div class="p-d b-f">
      <SearchComp :searchForm="searchForm" :searchData="queryString" :searchAction="searchAction"></SearchComp>
    </div>
    <div class="p-d b-f m-t">
      <div class="btnStyle flex-between ">
        <ButtonComp :buttonData="buttonData" />
      </div>
      <TableCompEle class="m-t" ref="multipleTable" :tableData="tableData" :tableConfig="tableConfig" :tableAction="tableAction" :isSelect="true"
        :isIndex="true" :isShowPage="true" :queryParams="queryParams" @getPagination="getList" @handleSelectionChange="handleSelectionChange">
      </TableCompEle>
    </div>
    <Add ref="addRef" @close="getList" />
  </div>
</template>

<script>
import { getRequirement, deletes } from '@/api/requirement.js'
import Add from './common/add.vue'
export default {
  components: { Add },
  data() {
    return {
      multipleSelection: [],
      queryParams: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },

      buttonData: [
        {
          kind: 'button',
          label: '新增',
          plain: false,
          size: 'small',
          type: 'primary',
          click: () => {
            this.add()
          }
        },
        {
          kind: 'button',
          label: '批量删除',
          plain: false,
          size: 'small',
          type: 'danger',
          click: () => {
            if (this.multipleSelection.length == 0) {
              this.$message({
                type: 'warning',
                message: '请选择要删除的数据'
              })
              return
            }
            const ids = []
            this.multipleSelection.forEach(items => {
              ids.push(items.id)
            })
            this.deleteFun(ids)
          }
        }
      ],
      tableData: [],
      tableConfig: [
        {
          prop: 'name',
          label: '需求名称'
        },
        {
          prop: 'typeName',
          label: '需求类别'
        },
        {
          prop: 'domainName',
          label: '领域名称'
        },
        {
          prop: 'reporter',
          label: '提报人'
        },
        {
          prop: 'departmentName',
          label: '单位'
        }
      ],
      tableAction: {
        align: () => 'center',
        width: () => String(160),
        operations: [
          {
            label: '详情',
            isLink: true,
            type: 'primary',
            method: row => {
              const obj = {
                title: '详情',
                data: row
              }
              this.$refs.addRef.show(obj)
            }
          },
          {
            label: '修改',
            isLink: true,
            type: 'primary',
            method: row => {
              const obj = {
                title: '修改',
                data: row
              }
              this.$refs.addRef.show(obj)
            }
          },
          {
            label: '删除',
            isLink: true,
            type: 'danger',
            method: row => {
              this.deleteFun([row.id])
            }
          }
        ]
      },
      queryString: {
        name: '',
        domainName: ''
      },
      searchForm: {
        changeMethod: () => {
          this.queryParams.currentPage = 1
          this.getList()
        },
        formData: [
          {
            type: 'Input',
            label: '需求名称',
            prop: 'name',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入需求名称'
          },
          {
            type: 'Input',
            label: '领域名称',
            prop: 'domainName',
            clearable: true,
            icon: 'el-icon-search',
            placeholder: '请输入领域名称'
          }
        ]
      },
      searchAction: [
        {
          label: '查询',
          type: 'primary',
          handle: () => {
            this.queryParams.currentPage = 1
            this.$refs.multipleTable.clearSelection()
            this.getList()
          }
        },
        {
          label: '重置',
          type: 'primary',
          plain: true,
          handle: () => {
            this.queryString = {
              name: '',
              domainName: ''
            }
            this.queryParams.currentPage = 1
            this.getList()
          }
        }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      getRequirement(
        Object.assign(
          {
            currentPage: this.queryParams.currentPage,
            pageSize: this.queryParams.pageSize
          },
          this.queryString
        )
      ).then(res => {
        this.queryParams.total = res?.data?.total ?? 0
        this.tableData = res?.data?.list ?? []
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteFun(ids) {
      this.$confirm(`确定要删除吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deletes({
            ids: ids
          }).then(() => {
            this.$refs.multipleTable.clearSelection()
            this.getList()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    add() {
      this.$refs.addRef.show({ title: '新增' })
    }
  }
}
</script>

<style lang="less" scoped>
</style>